package com.liuyoung.clockview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.Calendar;

/**
 * Created by Administrator on 2017/2/27.
 */

public class CalendarView extends View {

    private Paint mPaint;
    private Paint mTextPaint;

    private Bitmap mCalendarBitmap;

    private int mCalendarWidth;
    private int mCalendarHeight;

    public CalendarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCalendar();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStrokeWidth(6);
        mTextPaint.setTextSize(40);
//        mTextPaint.setColor(Color.RED);
    }

    private void initCalendar() {
        mCalendarBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.calendar);
        measuereBitmap();
    }

    private void measuereBitmap() {
        mCalendarWidth = mCalendarBitmap.getWidth();
        mCalendarHeight = mCalendarBitmap.getHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mCalendarBitmap, 0, 0, mPaint);
        canvas.save();

        Calendar c = Calendar.getInstance();

        int week = c.get(Calendar.DAY_OF_WEEK);
        String weekString = "";
        switch (week){
            case Calendar.SUNDAY:weekString="周日";break;
            case Calendar.SATURDAY:weekString="周六";break;
            case Calendar.FRIDAY:weekString="周五";break;
            case Calendar.THURSDAY:weekString="周四";break;
            case Calendar.WEDNESDAY:weekString="周三";break;
            case Calendar.TUESDAY:weekString="周二";break;
            case Calendar.MONDAY:weekString="周一";break;
        }

        Rect bounds = new Rect();
        mTextPaint.getTextBounds(weekString,0,weekString.length(),bounds);
        Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
        int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 4 - fontMetrics.top;
        canvas.drawText(weekString,getMeasuredWidth() / 2 - bounds.width() / 2, baseline, mTextPaint);
        canvas.save();

        int day = c.get(Calendar.DAY_OF_MONTH);
        String dayString = day+"日";
        mTextPaint.setTextSize(50);
        mTextPaint.getTextBounds(dayString,0,dayString.length(),bounds);
        baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top)/ 2 - fontMetrics.top*2;
        canvas.drawText(dayString,getMeasuredWidth() / 2 - bounds.width() / 2, baseline, mTextPaint);
        canvas.save();

    }

    private int measureWidth(int mode, int width) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                mCalendarWidth = width;
                Log.d("abc","width "+mCalendarWidth);
                Log.d("abc","height "+mCalendarHeight);
                mCalendarBitmap   = Bitmap.createScaledBitmap(mCalendarBitmap,mCalendarWidth,mCalendarHeight,true);
                break;
        }
        return mCalendarWidth;
    }

    private int measureHeight(int mode, int height) {
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                mCalendarHeight = height;
                break;
        }
        return mCalendarHeight;
    }

}